home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / SHELL_SO / PATHEXPA.C < prev    next >
Text File  |  1992-12-02  |  11KB  |  484 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 030190    :    Update for Mac, DOS paths
  54.  *    SPK 012290    :    Initial
  55.  */
  56.  
  57. #include    "System.h"
  58. #include    "Global.h"
  59. #include    "Mac.h"
  60. #include    "Path.h"
  61.  
  62. #include    "Prefs.h"
  63.  
  64. #include    <FileMgr.h>
  65. #include    <HFS.h>
  66.  
  67. /*******************************************************************
  68.  *    
  69.  *    ExpandPath takes a path pattern and expands it to all of its
  70.  *    possible values.  For each match that it makes it calls  the    
  71.  *    callBack procdure in the following format
  72.  *    
  73.  *    callBack( WHandle ShellWh, int ProcID, char *path, char *last,
  74.  *       pathType what, int vRefNum, long dirID );
  75.  *    
  76.  *    if what == pathIsDir then
  77.  *        path is the dirs name
  78.  *        vRefNum and dirID are for the dir
  79.  *    
  80.  *    if what == pathIsFile then
  81.  *        path is the file name
  82.  *        vRefNum and dirID are for the dir of the file
  83.  *    
  84.  *******************************************************************/
  85.  
  86. /* move these to shell data object */
  87.  
  88. char        expanded[ 256 ];    /* the expanded pathname */
  89. int16        matches;            /* number matches to the current path */
  90. Boolean        abortSearch = FALSE;
  91. Boolean        scanFwd    = TRUE;
  92. OsType        scanTYPE = 0L,
  93.             scanCREA = 0L;
  94. char        strTYPE[ 8 ],
  95.             strCREA[ 8 ];
  96.             
  97. void    ScanExpFwd( Boolean forward )
  98. {
  99.     scanFwd = forward;
  100. }
  101.  
  102. void    ScanForTYPE( OsType type )
  103. {
  104.     scanTYPE = type;
  105.     TypeToStr( type, strTYPE );
  106. }
  107.  
  108. void    ScanForCREA( OsType creator )
  109. {
  110.     scanCREA = creator;
  111.     TypeToStr( creator, strCREA );
  112. }
  113.  
  114. void    ScanInit()
  115. {
  116.     scanFwd = TRUE;
  117.     scanTYPE = 0L;
  118.     scanCREA = 0L;
  119. }
  120.  
  121. Boolean        ExpandCheck( OsType type, OsType creator )
  122. {
  123.     if( scanTYPE || scanCREA )
  124.         {
  125.         if( scanTYPE && (scanTYPE != type ))
  126.             {
  127.             char    str[ 8 ];
  128.             
  129.             TypeToStr( type, str );
  130.             
  131.             if( StrPatMatch( str, strTYPE ) == FALSE )
  132.                 return( FALSE );        
  133.             }
  134.             
  135.         if( scanCREA && (scanCREA != creator ))
  136.             {
  137.             char    str[ 8 ];
  138.             
  139.             TypeToStr( creator, str );
  140.             
  141.             if( StrPatMatch( str, strCREA ) == FALSE )
  142.                 return( FALSE );        
  143.             }
  144.         }
  145.         
  146.     return( TRUE );        /* no finder check */ 
  147. }
  148.  
  149. /*******************************************************************/
  150.  
  151. void        ExpandDirPath( WHandle ShellWh, int16 ProcID, char *pattern,
  152.                          ProcPtr callBack, int16 vRefNum, int32 dirID )
  153. {
  154. int16        len, i = 0, inc, recurse = FALSE, movedUp = 0;
  155. int32        parDirID;
  156. char        nextEntry[ 64 ], scan[ 64 ], *cp, *lastPat, *recursePat;
  157. pathType    pt;
  158. OSErr        err;
  159. CInfoPBRec    scanPB;
  160. OsType        type, creator;
  161.  
  162.     if( abortSearch || UserAbort() )
  163.         {
  164.         abortSearch = TRUE;
  165.         return;
  166.         }
  167.  
  168.     len = strlen( expanded );
  169.     if( *pattern == '\0' )    /* no spec */
  170.         return;
  171.                 
  172.     if( *pattern == ':')            /* look for Mac dir separator */
  173.         {
  174.         if( *(pattern+1) == ':' )    /* look for parent spec */
  175.             while( *pattern == ':' && *(pattern+1) == ':' )
  176.                 {
  177.                 CursorWait();
  178.                 if( err = GetParID( vRefNum, dirID, &parDirID ) )
  179.                     {
  180.                     FileError( err );
  181.                     return;
  182.                     }
  183.                     
  184.                 dirID = parDirID;
  185.                 movedUp++;
  186.                 
  187.                 if( err = GetParID( vRefNum, dirID, &parDirID ) )
  188.                     {
  189.                     FileError( err );
  190.                     return;
  191.                     }
  192.                 pattern++;    
  193.                 pattern++;    
  194.                 }
  195.         else
  196.             pattern++;
  197.         }
  198.     else 
  199.         return;
  200.     
  201.     if( *pattern =='\0' && callBack )    /* last spec was a dir */
  202.         {
  203.         matches++;
  204.  
  205.         GetCurrMacPath( vRefNum, dirID, expanded );            
  206.         (*callBack)( ShellWh, ProcID, expanded, "", pathIsDir,
  207.             vRefNum, dirID );
  208.         return;
  209.         }
  210.         
  211.     lastPat = pattern;
  212.     lastPat--;
  213.     
  214.     while( (*pattern != ':') && *pattern )    /* save entry */
  215.         nextEntry[i++] = *pattern++;
  216.         
  217.     nextEntry[i] = '\0';
  218.     
  219.     if( (strcmp( nextEntry, "..." ) == 0) || /* was it a recursive spec ? */
  220.         (strcmp( nextEntry, "╔" ) == 0) )
  221.         recurse = TRUE;
  222.     
  223.     /*    Get the number of items in this directory */
  224.     if( scanFwd == FALSE )
  225.         {
  226.         scanPB.dirInfo.ioFDirIndex     = -1;    /* this directory */
  227.         scanPB.dirInfo.ioCompletion = 0L; 
  228.         scanPB.dirInfo.ioNamePtr    = (StringPtr) scan;
  229.         scanPB.dirInfo.ioVRefNum     = vRefNum;
  230.         scanPB.dirInfo.ioDrDirID    = dirID;
  231.         
  232.         err = PBGetCatInfo( &scanPB, FALSE );
  233.         
  234.         i = scanPB.dirInfo.ioDrNmFls;
  235.         inc = (-1);
  236.         }
  237.     else
  238.         {
  239.         inc = 1;
  240.         i = 1;
  241.         }
  242.  
  243.     while( i )        /* for each item index ... */
  244.         {
  245.         CursorWait();
  246.         if( abortSearch || UserAbort() )
  247.             {
  248.             abortSearch = TRUE;
  249.             return;
  250.             }
  251.             
  252.         scanPB.hFileInfo.ioFDirIndex     = i;
  253.         scanPB.hFileInfo.ioCompletion     = 0L; 
  254.         scanPB.hFileInfo.ioNamePtr        = (StringPtr) scan;
  255.         scanPB.hFileInfo.ioVRefNum         = vRefNum;
  256.         scanPB.hFileInfo.ioDirID        = dirID;
  257.         
  258.         err = PBGetCatInfo( &scanPB, FALSE );
  259.  
  260.         if( err == fnfErr )        /* last item */
  261.             break;
  262.  
  263.         else if( err )
  264.             {
  265.             FileError( err );
  266.             break;
  267.             }
  268.  
  269.         type    = scanPB.hFileInfo.ioFlFndrInfo.fdType;
  270.         creator    = scanPB.hFileInfo.ioFlFndrInfo.fdCreator;
  271.         
  272.         PtoCstr( scan );
  273.         if( recurse )            /* depth first */
  274.             {
  275.             long     scanDirID = scanPB.hFileInfo.ioDirID;
  276.             char    nextDown[ 256 ];
  277.             int        j = 0;
  278.             
  279.             recursePat = pattern;
  280.             while( *recursePat == ':')
  281.                 recursePat++;
  282.                     
  283.             while( (*recursePat != ':') && *recursePat )
  284.                 nextDown[j++] = *recursePat++;
  285.         
  286.             nextDown[j] = '\0';
  287.                         
  288.             if( StrPatMatch( scan, nextDown ) )
  289.                 {
  290.                 if( scanPB.hFileInfo.ioFlAttrib & 0x10 )
  291.                     {
  292.                     cp = expanded + len;    /* add this name to the expanded path */
  293.                     *cp++ = ':';    *cp = '\0';
  294.                     strcat( cp, scan );
  295.                     
  296.                     if( *pattern )
  297.                         ExpandDirPath( ShellWh, ProcID, pattern,
  298.                             callBack, vRefNum, scanDirID );
  299.                         
  300.                     else if( callBack && ExpandCheck( 'Fldr', 'Fldr' ))
  301.                         {
  302.                         matches++;
  303.                         (*callBack)( ShellWh, ProcID, expanded, scan,
  304.                             pathIsDir, vRefNum, dirID );
  305.                         }
  306.                     
  307.                     expanded[ len ] = '\0';        /* reset back to path */
  308.                     }
  309.                 else if( *recursePat == '\0' && callBack &&
  310.                             ExpandCheck( type, creator ))
  311.                     {
  312.                     matches++;
  313.                     (*callBack)( ShellWh, ProcID, expanded, scan,
  314.                         pathIsFile, vRefNum, dirID );
  315.                     }
  316.                 }
  317.             else if( scanPB.hFileInfo.ioFlAttrib & 0x10 )
  318.                 {
  319.                 cp = expanded + len;/* add this name to the expanded path */
  320.                 *cp++ = ':';    *cp = '\0';
  321.                 strcat( cp, scan );
  322.                 
  323.                 if( j == 0 ) 
  324.                     strcpy( nextDown, "*" );
  325.     
  326.                 ExpandDirPath( ShellWh, ProcID, lastPat,
  327.                     callBack, vRefNum, scanDirID );
  328.                 expanded[ len ] = '\0';        /* reset back to path */
  329.                 }
  330.             }
  331.         else if( StrPatMatch( scan, nextEntry ) )
  332.             {
  333.             if( scanPB.hFileInfo.ioFlAttrib & 0x10 )    /* was a directory */
  334.                 {
  335.                 long     scanDirID = scanPB.hFileInfo.ioDirID;
  336.                 cp = expanded + len;    /* add this name to the expanded path */
  337.                 *cp++ = ':';
  338.                 *cp = '\0';
  339.                 strcat( cp, scan );
  340.                 
  341.                 if( *pattern )
  342.                     ExpandDirPath( ShellWh, ProcID, pattern, callBack,
  343.                         vRefNum, scanDirID );
  344.                     
  345.                 else if( callBack && ExpandCheck( 'Fldr', 'Fldr' ))
  346.                     {
  347.                     matches++;
  348.                     (*callBack)( ShellWh, ProcID, expanded, scan,
  349.                         pathIsDir, vRefNum, dirID );
  350.                     }
  351.                 
  352.                 expanded[ len ] = '\0';        /* reset back to path */
  353.                 }
  354.             else if( *pattern == '\0' && callBack &&
  355.                     ExpandCheck( type, creator ))
  356.                 {
  357.                 matches++;
  358.                 (*callBack)( ShellWh, ProcID, expanded, scan,
  359.                     pathIsFile, vRefNum, dirID );
  360.                 }
  361.             }
  362.             
  363.         i += inc;
  364.         }
  365. }
  366.  
  367. /*******************************************************************/
  368.  
  369. void        ExpandVolPath( WHandle ShellWh, int16 ProcID, char *pattern,
  370.                 char *path,  ProcPtr callBack  )
  371. {
  372. int16            i;
  373. OSErr            err;
  374. HVolumeParam    HPB;
  375. char            str[ 256 ];
  376.  
  377.     for(  i = 1; i < 10; i++ )
  378.         {
  379.         if( abortSearch || UserAbort() )
  380.             {
  381.             abortSearch = TRUE;
  382.             return;
  383.             }
  384.  
  385.         HPB.ioCompletion     = NULL;
  386.         HPB.ioNamePtr         = (StringPtr) str;
  387.         HPB.ioVRefNum         = 0;
  388.         HPB.ioVolIndex         = i;
  389.         
  390.         err = PBHGetVInfo( &HPB, FALSE );
  391.         
  392.         if( err == nsvErr )
  393.             break;
  394.             
  395.         if( err )
  396.             {
  397.             FileError( err );
  398.             break;
  399.             }
  400.  
  401.         PtoCstr( str );
  402.         strcpy( expanded, str );
  403.  
  404.         if( (strcmp( pattern, "..." ) == 0) || (strcmp( pattern, "╔" ) == 0) )
  405.             {
  406.             if( *path )
  407.                 {
  408.                 char    p2[ 256 ];
  409.                 
  410.                 p2[ 0 ] = ':';    p2[ 1 ] = '\0';
  411.                 strcat( p2, pattern );
  412.                 strcat( p2, path );
  413.                 
  414.                 ExpandDirPath( ShellWh, ProcID, p2, callBack,
  415.                     HPB.ioVRefNum, 2L );
  416.                 }
  417.             else if( callBack && ExpandCheck( 'Fldr', 'Fldr' ))
  418.                 {
  419.                 matches++;
  420.                 (*callBack)( ShellWh, ProcID, expanded, str,
  421.                     pathIsVol, HPB.ioVRefNum, 2L );
  422.                 }
  423.             }
  424.         else if( StrPatMatch( str, pattern ) )
  425.             {
  426.             if( *path )
  427.                 ExpandDirPath( ShellWh, ProcID, path, callBack,
  428.                     HPB.ioVRefNum, 2L );
  429.                     
  430.             else if( callBack && ExpandCheck( 'Fldr', 'Fldr' ))
  431.                 {
  432.                 matches++;
  433.                 (*callBack)( ShellWh, ProcID, expanded, str,
  434.                     pathIsVol, HPB.ioVRefNum, 2L );
  435.                 }
  436.             }
  437.         }
  438. }
  439.  
  440. /*******************************************************************/
  441.  
  442. int16        ExpandPath( WHandle ShellWh, int16 ProcID, char *expPattern,
  443.                     ProcPtr callBack, int16 vRefNum, int32 dirID )
  444. {
  445. Boolean        neededVol, goToRoot;
  446. char        volPattern[ 64 ],
  447.             pathPattern[ 256 ];
  448. int32        parDirID;
  449.  
  450.     CursorWait();
  451.     matches = 0;
  452.     expanded[ 0 ] = '\0';
  453.     abortSearch = FALSE;
  454.     
  455.     strcpy( pathPattern, expPattern );
  456.     
  457.     if( ShellPrefs.useMacOSPath )
  458.         _MacToMac( pathPattern, &neededVol, &goToRoot, volPattern );
  459.     else if( ShellPrefs.useUNIXPath )
  460.         _UNIXToMac( pathPattern, &neededVol, &goToRoot, volPattern );
  461.     else if( ShellPrefs.useDOSPath )
  462.         _DOSToMac( pathPattern, &neededVol, &goToRoot, volPattern );
  463.         
  464.     if( neededVol )
  465.         {
  466.         if( vRefNum == 0 )    /* volume does not exist */
  467.             return;
  468.             
  469.         ExpandVolPath( ShellWh, ProcID, volPattern, pathPattern, callBack );
  470.         }
  471.     else
  472.         {
  473.         if( goToRoot )
  474.             {
  475.             GetVol( NULL, &vRefNum );
  476.             dirID = 0;
  477.             }
  478.             
  479.         ExpandDirPath( ShellWh, ProcID, pathPattern, callBack, vRefNum, dirID );
  480.         }
  481.         
  482.     SetArrow();
  483.     return( matches );
  484. }